"""
Copyright 2012 Wieser Dietmar, Margit Mutschlechner.

This file is part of Multimedia Social Networking Platform.

Multimedia Social Networking Platform is free software: 
you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Multimedia Social Networking Platform is distributed
in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Multimedia Social Networking Platform.  
If not, see <http://www.gnu.org/licenses/>.

"""

import difflib

def isEquals(string1, string2):
    """Compares two strings for their matching score.
    
    The matching is case independent.
    Returns true if the two strings are equal or one is a substring of the other.
    If this is not the case the SequenceMatcher class of the difflib module is called.
    If its calculated matching score is above 0.8 it returns true, otherwise false.
    
    Args:
        string1: The first string to be compared.
        string2: The second string to be compared.
        
    Returns:
        It returns either true, if the two strings match, false otherwise.
    """
    if string1 == None or string2 == None:
        return False
    
    s1 = string1.lower()
    s2 = string2.lower()
    
    if len(s1) > 2 and len(s2) > 2:
        # check if first is substring of second
        if s1 in s2:
            return True
        
        # check if second is substring of first
        if s2 in s1:
            return True
    
    # check for nearly matching
    diff = difflib.SequenceMatcher(None, s1, s2).quick_ratio()
    
    if diff > 0.8:
        return True
    return False

